package com.neulogistics.contrib.procuredcenter.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.procuredcenter.api.application.command.CreateVendorCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.GoodsIdCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateVendorCommand;
import com.neulogistics.contrib.procuredcenter.api.application.query.IVendorQueryService;
import com.neulogistics.contrib.procuredcenter.api.application.query.VendorQuery;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.VendorViewModel;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "供应商管理")
@RequestMapping("/procuredCenter/vendor")
public class VendorController {
    @Resource
    IMediator mediator;

    @Resource
    IVendorQueryService vendorQueryService;

    @ApiOperation("新增供应商")
    @PostMapping("/create")
    public BaseResponse<String> create(@RequestBody @Validated @NotNull CreateVendorCommand command){

        BaseResponse<String> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("修改供应商")
    @PostMapping("/update")
    public BaseResponse update(@RequestBody @Validated @NotNull UpdateVendorCommand command){

        BaseResponse baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据id获取供应商")
    @GetMapping("/get/{vendorId}")
    public BaseResponse<VendorViewModel> get(@PathVariable(value = "vendorId") @NotNull @Positive Integer vendorId){

        VendorViewModel goodsSetViewModel = vendorQueryService.getById(vendorId);

        return ResultUtils.success(goodsSetViewModel);
    }

    @ApiOperation("根据商品id获取供应商信息")
    @PostMapping("/getByGoodsId")
    public BaseResponse<VendorViewModel> getByGoodsId(@RequestBody @NotNull GoodsIdCommand command){

        VendorViewModel vendorViewModel = vendorQueryService.getByGoodsId(command.goodsId);

        return ResultUtils.success(vendorViewModel);
    }

    @ApiOperation("分页查询供应商")
    @PostMapping("/page")
    public BaseResponse<Page<VendorViewModel>> page(@RequestBody @Validated @NotNull VendorQuery vendorQuery){

        Page<VendorViewModel> viewModelPage = vendorQueryService.page(vendorQuery);

        return ResultUtils.success(viewModelPage);
    }
}
